//
//  RegistrationViewController.m
//  WE
//
//  Created by apple on 15/11/18.
//  Copyright © 2015年 UNOHACHA. All rights reserved.
//

#import "RegistrationViewController.h"
#import "Size.h"
#import "CreatorModel.h"
#import "ToolUtil.h"
#import "RegistrationTableViewCell.h"
#import "CityViewController.h"
#import "CityModel.h"
#import "InfirmaryViewController.h"
#import "LiveCountModel.h"
#import "BaseView.h"
#import "Label+ImageButton.h"
#import "MJRefresh.h"
@interface RegistrationViewController ()<UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource>
{
    UIVisualEffectView *view;
    UISearchBar *searchBar1;
    UIView *sortView;
    UIView *sortView1;
    UIButton *cityButton;
    NSArray *hospitalArray;
    BaseView *blankView;
    UIView *screenView;
    UILabel *speedLabel;
    UILabel *rankLabel;
    BaseView *rubbishView;
    UIView *noUseView;
    Label_ImageButton *label;
    Label_ImageButton *imageButton;
    UIButton *bu;
    UIButton *butt;
}
@property (nonatomic, strong)UIButton *tmpBtn;
@property (nonatomic, strong)UIButton *mpBtu;
@end

@implementation RegistrationViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"预约挂号";
    self.view.backgroundColor = [UIColor whiteColor];
    CityModel *handle = [CityModel mainCityModel];
    handle.str = @"杭州";
   
    searchBar1 = [[UISearchBar alloc] initWithFrame:CGRectMake(5, 69, WindowWidth - 10, 40)];
    searchBar1.barStyle = UIBarStyleDefault;
    searchBar1.placeholder = @"搜索医院";
    searchBar1.delegate = self;
    searchBar1.searchBarStyle = UISearchBarStyleMinimal;
    [self.view addSubview:searchBar1];
    view = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
    view.frame = CGRectMake(0, 20, WindowWidth, WindowHeight);
    view.hidden = YES;
    UITapGestureRecognizer *doubleClick = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pressCancle)];
    doubleClick.numberOfTapsRequired = 1;
    [view addGestureRecognizer:doubleClick];
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
    cancelButton.frame = CGRectMake(WindowWidth - 60, 20, 50, 40);
    cancelButton.backgroundColor = [UIColor whiteColor];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(pressCancle) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:cancelButton];
    
    sortView = [CreatorModel initWithFrame:CGRectMake(0, 0, WindowWidth, WindowHeight - 80) hidden:YES view:self.view];
    UITapGestureRecognizer*  singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SingleTap)];
    //点击的次数
    singleRecognizer.numberOfTapsRequired = 1; // 单击
    [sortView addGestureRecognizer:singleRecognizer];
    
    
    sortView1 = [CreatorModel initWithFrame:CGRectMake(0, WindowHeight - 80, WindowWidth, 80) hidden:YES view:self.view];
    UITapGestureRecognizer*  singleRecognizer1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SingleTap1)];
    //点击的次数
    singleRecognizer1.numberOfTapsRequired = 1; // 单击
    [sortView addGestureRecognizer:singleRecognizer1];
    
    UIView *broundView = [CreatorModel initWithFrame:CGRectMake(0, WindowHeight - 49, WindowWidth, 49) hidden:NO view:self.view];
   // broundView.backgroundColor = [UIColor grayColor];
    [ToolUtil createLine:WindowHeight - 48 superView:self.view lift:0 width:WindowWidth hight:1 color:[UIColor grayColor]];
    
    cityButton = [UIButton buttonWithType:UIButtonTypeCustom];
    cityButton.frame = CGRectMake(0, 0, (WindowWidth - 1) / 3, 49);
    [cityButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    cityButton.titleLabel.font = FONT(12);
    [cityButton setTitle:[NSString stringWithFormat:@"%@ ▴",handle.str] forState:UIControlStateNormal];
    [cityButton addTarget:self action:@selector(pressCityButton:) forControlEvents:UIControlEventTouchUpInside];
    //[cityButton.layer setBorderWidth:1];
    [broundView addSubview:cityButton];
    
    [ToolUtil createVerticalLine:5 superView:broundView lift:(WindowWidth - 1) / 3 height:29];
    
    UIButton *sortButton = [UIButton buttonWithType:UIButtonTypeCustom];
    sortButton.frame = CGRectMake((WindowWidth - 1) / 3 + 0.5, 0, (WindowWidth - 2) / 3, 49);
    [sortButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];    [sortButton setTitle:@"综合排序 ▴" forState:UIControlStateNormal];
    sortButton.titleLabel.font = FONT(12);
    [sortButton addTarget:self action:@selector(pressSortButton) forControlEvents:UIControlEventTouchUpInside];
    [broundView addSubview:sortButton];
    
    [ToolUtil createVerticalLine:5 superView:broundView lift:(WindowWidth - 1) / 3 * 2 + 0.5 height:29];
    
    UIButton *screenButton = [UIButton buttonWithType:UIButtonTypeCustom];
    screenButton.frame = CGRectMake((WindowWidth - 1) / 3 * 2 + 1, 0, (WindowWidth - 2) / 3, 49);
   [screenButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    screenButton.titleLabel.font = FONT(12);
    [screenButton setTitle:@"筛选 ▴" forState:UIControlStateNormal];
    [screenButton addTarget:self action:@selector(pressScreenButton) forControlEvents:UIControlEventTouchUpInside];
    [broundView addSubview:screenButton];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 109, WindowWidth, WindowHeight - 159)];
    _tableView.showsVerticalScrollIndicator = NO;

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.view addSubview:self.tableView];
    hospitalArray = @[@"浙江省肿瘤医院",@"浙江省立同德医院",@"浙江医院",@"杭州医院",@"杭州骨科医院"];
    [self.view addSubview:view];
    [self setupScreenView];
//    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//        
//        
//        [_tableView.mj_header beginRefreshing];
//        
//    }];
}

- (void)setupScreenView{
    blankView = [[BaseView alloc] initWithFrame:CGRectMake(0, 20*Height, WindowWidth, (WindowHeight - 20)/5 *3)];
    [blankView addTarget:self selector:@selector(pressScreenView)];
    blankView.hidden = YES;
    blankView.backgroundColor = GRAYCOLOR;
    blankView.alpha = 0.6;
    [self.view addSubview:blankView];
    screenView = [[UIView alloc] initWithFrame:CGRectMake(0, (WindowHeight - 20)/5 *3 + 20*Height, WindowWidth, (WindowHeight - 20)/5 *2)];
    screenView.backgroundColor = [UIColor whiteColor];
    screenView.hidden = YES;
    [self.view addSubview:screenView];
    UIImageView *screenImageV = [[UIImageView alloc] initWithFrame:CGRectMake(20*Width, 20*Height, 20*Width, 20*Height)];
    screenImageV.image = [UIImage imageNamed:@"iconfont-screen"];
    [screenView addSubview:screenImageV];
    UILabel *screenLabel = [[UILabel alloc] initWithFrame:CGRectMake(50*Width, 10*Height, 40*Width, 40*Height)];
    screenLabel.text = @"筛选";
    screenLabel.font = FONT(14);
    screenLabel.textColor = [UIColor blackColor];
    [screenView addSubview:screenLabel];
    
    UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    confirmButton.frame = CGRectMake(WindowWidth - 70*Width, 20*Height, 50*Width, 20*Height);
    [confirmButton setTitle:@"确定" forState:UIControlStateNormal];
    confirmButton.titleLabel.font = FONT(14);
    confirmButton.backgroundColor = [UIColor greenColor];
    [confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [confirmButton addTarget:self action:@selector(pressconfirmButton) forControlEvents:UIControlEventTouchUpInside];
    [screenView addSubview:confirmButton];
    [ToolUtil createLine:60*Height superView:screenView lift:0 width:WindowWidth hight:1*Height color:[UIColor grayColor]];
    
    UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(20*Width, 80*Height, WindowWidth / 6 + 20 *Height, 20*Height)];
    timeLabel.text = @"候诊时间 :";
    timeLabel.font = FONT(14);
    timeLabel.backgroundColor = [UIColor whiteColor];
    timeLabel.textColor = [UIColor blackColor];
    [screenView addSubview:timeLabel];
    
    speedLabel = [[UILabel alloc] initWithFrame:CGRectMake(WindowWidth/6 + 30 *Width + WindowWidth / 30, 80*Height, WindowWidth / 6, 20*Height)];
    speedLabel.font = FONT(14);
    speedLabel.text = @"不限";
    speedLabel.textColor = [UIColor blueColor];
    speedLabel.backgroundColor = [UIColor whiteColor];
    [screenView addSubview:speedLabel];
    
    NSArray *arr = @[@"不限",@"非常快",@"比较快",@"一般"];
    for (int i = 0; i < arr.count; i++) {
        UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
        but.frame = CGRectMake(WindowWidth / 30 * (i + 1)+ WindowWidth/ 5 *i, 110*Height, WindowWidth / 5, 20*Height);
        but.backgroundColor = [UIColor colorWithRed:0.8371 green:0.8371 blue:0.8371 alpha:1.0];
        [but setTitle:arr[i] forState:UIControlStateNormal];
        [but setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        [but setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        but.titleLabel.font = FONT(14);
        [but addTarget:self action:@selector(pressbut:) forControlEvents:UIControlEventTouchUpInside];
        [screenView addSubview:but];
    }
    [ToolUtil createLine:150*Height superView:screenView lift:0 width:WindowWidth hight:1*Height color:[UIColor grayColor]];
    
    UILabel *levelLabel = [[UILabel alloc] initWithFrame:CGRectMake(20*Width, 170*Height, WindowWidth / 6 + 20 *Height, 20*Height)];
    
    levelLabel.text = @"医院级别 :";
    levelLabel.font = FONT(14);
    levelLabel.backgroundColor = [UIColor whiteColor];
    levelLabel.textColor = [UIColor blackColor];
    [screenView addSubview:levelLabel];
    rankLabel = [[UILabel alloc] initWithFrame:CGRectMake(WindowWidth/6 + 30 *Width + WindowWidth / 30, 170*Height, WindowWidth / 6, 20*Height)];
    rankLabel.font = FONT(14);
    rankLabel.text = @"不限";
    rankLabel.textColor = [UIColor blueColor];
    rankLabel.backgroundColor = [UIColor whiteColor];
    [screenView addSubview:rankLabel];
    
    NSArray *array = @[@"不限",@"三级甲等"];
    for (int i = 0; i < array.count; i++) {
        UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
        but.frame = CGRectMake(WindowWidth / 30 * (i + 1)+ WindowWidth/ 5 *i, 210*Height, WindowWidth / 5, 20*Height);
        but.backgroundColor = [UIColor colorWithRed:0.8371 green:0.8371 blue:0.8371 alpha:1.0];
        [but setTitle:array[i] forState:UIControlStateNormal];
        [but setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        [but setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        but.titleLabel.font = FONT(14);
        [but addTarget:self action:@selector(pressLevelButton:) forControlEvents:UIControlEventTouchUpInside];
        [screenView addSubview:but];
    }
    
    
    rubbishView = [[BaseView alloc] initWithFrame:CGRectMake(0, 20, WindowWidth, (WindowHeight - 20)/5 *4)];
    [rubbishView addTarget:self selector:@selector(pressRubbishView)];
    rubbishView.hidden = YES;
    rubbishView.backgroundColor = [UIColor blackColor];
    rubbishView.alpha = 0.6;
    [self.view addSubview:rubbishView];

    noUseView = [[UIView alloc] initWithFrame:CGRectMake(0, (WindowHeight - 20)/5 *4, WindowWidth, (WindowHeight - 20)/5 + 20)];
    noUseView.hidden = YES;
    NSLog(@"%lf",noUseView.frame.size.height);
    noUseView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:noUseView];
    
    label = [Label_ImageButton buttonWithType:UIButtonTypeCustom];
    bu  = [label setupButtonFrame:CGRectMake(0, 0, WindowWidth, noUseView.frame.size.height / 2) title:@"综合排序" image:[UIImage imageNamed:@"iconfont-screen"]];
    
    [noUseView addSubview:bu];
    [bu addTarget:self action:@selector(pressLabel_Image:) forControlEvents:UIControlEventTouchUpInside];
    
    [ToolUtil createLine:noUseView.frame.size.height / 2 superView:noUseView lift:15*Height width:WindowWidth - 15*Height hight:1*Height color:[UIColor grayColor]];
    
    imageButton = [Label_ImageButton buttonWithType:UIButtonTypeCustom];
    
    
    butt = [imageButton setupButtonFrame:CGRectMake(0, noUseView.frame.size.height / 2 +1*Height, WindowWidth, noUseView.frame.size.height / 2 - 1*Height) title:@"预约量" image:[UIImage imageNamed:@"iconfont-right"]];
    [noUseView addSubview:butt];
    
    [butt addTarget:self action:@selector(pressLabel_Image:) forControlEvents:UIControlEventTouchUpInside];

}




- (void)pressLabel_Image:(UIButton*)imagebtu{
    
    if (bu == imagebtu) {
        imageButton.imageV.image = [UIImage imageNamed:@"iconfont-screen"];
        label.imageV.image = [UIImage imageNamed:@"iconfont-right"];
        rubbishView.hidden = YES;
        noUseView.hidden = YES;
    } else {
        label.imageV.image = [UIImage imageNamed:@"iconfont-screen"];
        imageButton.imageV.image = [UIImage imageNamed:@"iconfont-right"];
        rubbishView.hidden = YES;
        noUseView.hidden = YES;
    }
    
}

- (void)pressRanking{
    
}

- (void)pressRubbishView{
    rubbishView.hidden = YES;
    noUseView.hidden = YES;
}


- (void)pressLevelButton:(UIButton *)button{
    
    
    
    if (_mpBtu == nil){
        button.selected = YES;
        _mpBtu = button;
        button.backgroundColor = [UIColor blueColor];
    }
    else if (_mpBtu !=nil && _mpBtu == button){
        button.selected = YES;
        button.backgroundColor = [UIColor blueColor];
        
    }
    else if (_mpBtu!= button && _mpBtu!=nil){
        _mpBtu.selected = NO;
        _mpBtu.backgroundColor = [UIColor colorWithRed:0.8371 green:0.8371 blue:0.8371 alpha:1.0];
        button.selected = YES;
        button.backgroundColor = [UIColor blueColor];
        
        _mpBtu = button;
    }
    
    rankLabel.text = button.titleLabel.text;
}

- (void)pressbut:(UIButton *)btu{
        if (_tmpBtn == nil){
        btu.selected = YES;
        _tmpBtn = btu;
        btu.backgroundColor = [UIColor blueColor];
    }
    else if (_tmpBtn !=nil && _tmpBtn == btu){
        btu.selected = YES;
        btu.backgroundColor = [UIColor blueColor];
        
    }
    else if (_tmpBtn!= btu && _tmpBtn!=nil){
        _tmpBtn.selected = NO;
        _tmpBtn.backgroundColor = [UIColor colorWithRed:0.8371 green:0.8371 blue:0.8371 alpha:1.0];
        btu.selected = YES;
        btu.backgroundColor = [UIColor blueColor];
        
        _tmpBtn = btu;
    }
    
    speedLabel.text = btu.titleLabel.text;
        
}

- (void)pressconfirmButton{
    screenView.hidden = YES;
    blankView.hidden = YES;
}

- (void)pressScreenView{
    screenView.hidden = YES;
    blankView.hidden = YES;
    
}
- (void)pressScreenButton{
    blankView.hidden = NO;
    screenView.hidden = NO;
    
}

- (void)pressSortButton{
    
    rubbishView.hidden = NO;
    noUseView.hidden = NO;
    
}

- (void)pressCityButton: (UIButton*)button{
    
    
        [self presentViewController:[[CityViewController alloc] init] animated:YES completion:nil];
    
    
}

- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = NO;
    CityModel *handle = [CityModel mainCityModel];
    
    
    [cityButton setTitle:[NSString stringWithFormat:@"%@▲",handle.str] forState:UIControlStateNormal];
    
}

- (void)SingleTap{
    
}
- (void)SingleTap1{
    
}

- (void)pressCancle{
    
    searchBar1.frame = CGRectMake(5, 69, WindowWidth - 10, 40);
    [self.view addSubview:searchBar1];
    view.hidden = YES;
    self.navigationController.navigationBar.hidden = NO;
    [searchBar1 resignFirstResponder];
    searchBar1.searchBarStyle = UISearchBarStyleMinimal;
}


- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    
    self.navigationController.navigationBar.hidden = YES;
    [view addSubview:searchBar1];
    searchBar.frame = CGRectMake(10, 20, WindowWidth - 70, 40);
    searchBar.searchBarStyle = UISearchBarStyleMinimal;
    view.hidden = NO;
    
    
   // [_tableView.mj_header endRefreshing];
    
    return YES;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellString = @"RegistrationTableViewCell";
    
    RegistrationTableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:cellString];
    if (!cell) {
        cell = [[RegistrationTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellString];
        
        cell.imageV.image = [UIImage imageNamed:@"19D72705-8904-433C-BBA4-299342AFF304"];
        cell.nameLabel.text = hospitalArray[indexPath.row];
        cell.numberLabel.text = @"预约量2222";
        cell.levelLabel.text = @"三级甲等";
        
    }
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    InfirmaryViewController *inVC = [[InfirmaryViewController alloc] init];
    [self.navigationController pushViewController:inVC animated:YES];
    inVC.string = hospitalArray[indexPath.row];
    LiveCountModel *handle = [LiveCountModel mainLiveCountModel];
    handle.string = @"1";
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 5;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
